<?php

namespace App\Http\Controllers\Invoice\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Invoice\InvoiceValidate;
use App\Service\Invoice\InvoiceService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;


class InvoiceController extends BaseController
{

    /** 邮件转发
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Exception
     */
    public function transmitEmail(Request $request,$id): JsonResponse
    {
        $shopId  = (int)$request->header('shopId');
        $brandId = (int)$request->header('brandId');
        $userId  = getRmsUserId();

        // 追加id
        $request->merge(["id" => $id]);
        $request->merge(["userId" => $userId]);
        $request->merge(["shopId" => $shopId]);
        list($status,$msg) = InvoiceService::transmitEmail($id,$shopId,$request->post('email'));
        if($status){
            return $this->success();
        }else{
            return $this->fail(500,$msg);
        }

    }


    /**
     * 申请开票
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Exception|GuzzleException
     */
    public function applyInvoice(Request $request): JsonResponse
    {
        InvoiceService::applyInvoice(
            InvoiceValidate::applyInvoiceValidate(
                $request->all()),
            $request->header("shopId"),
            request()->user()->id
        );
        return $this->success();
    }

    /**
     * 红冲
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws GuzzleException
     */
    public function reApplyInvoice(Request $request): JsonResponse
    {
        InvoiceService::appReApplyInvoice(InvoiceValidate::reApplyInvoiceValidate($request->all()), request()->user()->id);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function list(Request $request): JsonResponse
    {
        return $this->success(
            InvoiceService::list(
                InvoiceValidate::listValidate($request->all()),
                $request->header("shopId")
            )
        );
    }

    /** 发票详情
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function info(Request $request,$id): JsonResponse
    {
        return $this->success(
            InvoiceService::info(
                $id,
                $request->header("shopId")
            )
        );
    }
}
